package com.yiwenup.leetcode.offer;

/**
 * https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
 **/
public class No005 {
    /**
     * 3 ms	36.6 MB
     */
    public String replaceSpace(String s) {
        return s.replaceAll(" ", "%20");
    }

    /**
     * 0 ms	36 MB
     */
    public String replaceSpace1(String s) {
        return s.replace(" ", "%20");
    }

    /**
     * 0 ms	36.2 MB
     */
    public String replaceSpace2(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                sb.append("%20");
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 0 ms	36 MB
     */
    public String replaceSpace3(String s) {
        int length = s.length();
        char[] array = new char[length + (length << 1)];
        int newSize = 0;
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                array[newSize++] = '%';
                array[newSize++] = '2';
                array[newSize++] = '0';
            } else {
                array[newSize++] = c;
            }
        }
        return new String(array, 0, newSize);
    }
}
